﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellCellet_h
#define CellCellet_h

#include "CellPrerequisites.h"
#include "CellLicense.h"
#include "CellGene.h"
#include "CellCelletFeature.h"
#include "CellObservable.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

typedef void (*DLL_START_CELLET)(void);
typedef void (*DLL_STOP_CELLET)(void);

#define START_CELLET_SYMBOL "startCellet"
#define STOP_CELLET_SYMBOL "stopCellet"

/** Cellet 事件
*/
struct CelletEvent
{
	static const String JOIN;
	static const String DETACH;
};

/** Cellet 是 Cell Cloud 系统中最基础的云服务单元。
任何加入云的服务都应当从 Cellet 继承。
@author
	Jiangwei Xu
*/
class _CellExport Cellet : public Observable, public CelletAlloc
{
private:
	/** 默认构造函数。
	*/
	Cellet(void);

public:
	/** 构造函数。
	*/
	Cellet(const CelletFeature& feature);

	/** 构造函数。
	*/
	Cellet(const CelletFeature& feature, const License& license);

	/** 析构函数。
	*/
	virtual ~Cellet(void);

public:
	/** 返回 Cellet 特性。
	*/
	const CelletFeature& getFeature(void) const;

	/** 注入基因。
	@param managed 是否对基因对象进行内存托管。
	*/
	void join(Gene* gene, bool managed = false);

	/** 丢弃基因。
	*/
	void detach(Gene* gene);

	/** 检索基因。
	*/
	Gene* search(const String& code);

	/** 枚举基因。
	*/
	void enumerate(GeneList& list);

	/** 添加启动子。
	*/
	void addPromoter(Promoter* promoter);

	/** 移除启动子。
	*/
	void removePromoter(Promoter* promoter);

	/** 使用原语进行对话。
	*/
	bool talk(const String& targetTag, Primitive& primitive);

	/** 使用方言进行对话。
	*/
	bool talk(const String& targetTag, Dialect& dialect);

private:
	/**
	*/
	void destroyGene(Gene* gene);

public:
	/**
	*/
	virtual void activate(void) = 0;

	/**
	*/
	virtual void deactivate(void) = 0;

public:
	/** 与内核进行会话。
	*/
	virtual void dialogue(const String& tag, const Primitive& primitive) = 0;

	/** 与对端 Nucleus 建立连接。
	*/
	virtual void contacted(const String& tag) = 0;

	/** 与对端 Nucleus 断开连接。
	*/
	virtual void quitted(const String& tag) = 0;

protected:
	CelletFeature mFeature;
	License mLicense;

private:
	typedef stx::btree_map<String, Gene*> Genes;
	Genes mGenes;
	CELL_MUTEX(mGeneMutex);

	friend class Nucleus;
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellCellet_h
